import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { WeaTools } from 'ecCom';
import { message } from 'antd';
import * as Apis from '../apis/contacterQuery';
import { CrmContactStore } from './crmContactStore';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export class ContacterViewStore {
    @observable title = '';
    @observable tabDatas = [];
    @observable contacterId = '';
    @observable customerId = '';
    @observable loading = false;
    @observable detailTabKey = '1';
    @observable canEdit = true;
    @observable form = new WeaForm();
    @observable condition = [];
    @observable trailinfo = [];

    @observable crmContactStore = new CrmContactStore();

    @observable verified = false;
    @observable hasRight = false; // 权限

    @observable rightMenus = false; // 右键菜单
    @observable showShareDialog = false; // 显示分享 弹框
    @observable userId = ''; // 显示分享 弹框
    @observable onlyView = false;

    @action
    initContacterInfo=(params = {}) => {
      this.contacterId = params.contacterId;
      this.loading = true;
      Apis.getContacterForm(params).then((data) => {
        this.initContacterInfoCallBack(params, data);
      });
    }

    @action
    initContacterInfoCallBack(params, data) {
      if (!data.hasright && typeof (data.hasright) !== 'undefined') {
        this.hasRight = data.hasright;
        this.verified = true;
      } else {
        this.hasRight = true;
        this.verified = true;
        this.condition = data.datas;
        this.title = data.title,
        this.customerId = data.customerId,
        this.rightMenus = data.rcList,
        this.userId = data.loginid;
        this.onlyView = data.onlyView;
        // 根据高级搜索条件初始化form
        this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        this.canEdit = data.canEdit;
        this.loading = false;
        if (params.hasOwnProperty('type')) {
          this.detailTabKey = params.type;
          if (this.detailTabKey == '2') {
            this.loading = true;
            Apis.getTrailInfo({ contacterId: this.contacterId }).then((data) => {
              if (data.status == 'success') {
                this.trailinfo = data.datas;
                this.loading = false;
              } else {
                message.error(getLabel(83437, '出现错误！') + data.msgcode);
              }
            }).catch(err => message.error(err));
          }
        }
      }
    }
    getRightMenus = () => {
      const { rightMenus } = this;
      const that = this;
      const btnArr = [];
      rightMenus && rightMenus.length > 0 && rightMenus.map((m) => {
        btnArr.push({
          key: m.type,
          icon: <i className={m.menuIcon} />,
          content: m.menuName,
          disabled: false,
          onClick: (key) => {
            if (key == 'BTN_SHARE') {
              that.controlShareDialog(true);
            }
          },
        });
      });
      return btnArr;
    }
    controlShareDialog =(bool) => {
      this.showShareDialog = bool;
    }
    // 实时编辑
    contacterEdit = (params = {}) => {
      const that = this;
      Apis.contacterEdit(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel(83551, '保存成功！'));
          that.setFieldValueOld(params.fieldName, params.newValue);
          if (params.fieldName == 'firstname') {
            that.title = params.newValue;
          }
        } else {
				 message.error(`操作失败,${data.msgcode}!`);
        }
      });
    }
    changeTab=(key) => {
      this.detailTabKey = key;
      if (key == '2') {
        this.loading = true;
        Apis.getTrailInfo({ contacterId: this.contacterId }).then((data) => {
          if (data.status == 'success') {
            this.trailinfo = data.datas;
            this.loading = false;
          } else {
            message.error(getLabel(83437, '出现错误！') + data.msgcode);
          }
        }).catch(err => message.error(err));
      }
    }
    // 删除联系人
    deleteContracter=(id) => {
      Apis.deleteContracter({ contacterId: id }).then((data) => {
        if (data.status == 'success') {
          if (window.opener._table !== undefined) {
            if (typeof window.opener._table.reLoad === 'function') { window.opener._table.reLoad(); }
          }
          window.close();
        } else {
				 message.error(`操作失败,${data.msgcode}!`);
        }
      });
    }
    clearStatus =() => {
      this.contacterId = '';
      this.customerId = '';
      this.detailTabKey = '1';
      this.condition = [];
      this.trailinfo = [];
    }
    @action
    setFieldValue=(fieldName, fieldValue, fieldValueSpan, fit) => {
      try {
        const field = {};
        if (fit == 'BROWSER') {
          field[fieldName] = { value: fieldValue, valueSpan: fieldValueSpan };
        } else {
          field[fieldName] = { value: fieldValue };
        }
        this.form.updateFields(field, false);
      } catch (error) {
        // 表单中找不到字段的情况
        // console.log(error)
      }
    }
    @action
    setFieldValueOld(fieldName, fieldValue) {
      const conditioninfo = this.condition;
      for (let i = 0; i < conditioninfo.length; i++) {
        const items = conditioninfo[i].items;
        for (let j = 0; j < items.length; j++) {
          const item = items[j];
          const name = item.domkey[0];
          if (fieldName == name) {
            item.value = fieldValue;
          }
        }
      }
    }
}

